﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using ST.Buz.Base.Entity;
using ST.Buz.Base.Enums;
using ST.Buz.Base.ViewModel;
using ST.Buz.Service;
using ST.Common.Data;
using ST.Common.Util;
using ST.Common.Util.Model;
using ST.Common.Util.Mvc;
using ST.Common.Util.Security;
using ST.Common.Util.Text;
using ST.WX.XCX.Model;
using System;
using System.Collections.Generic;
using System.Net;

namespace ST.Buz.Api.Controllers
{
	[Route("sys/user")]
	public class Sys_UserController : BaseJsonController
	{
		private readonly IServiceSysUser service;
		private readonly IServiceSysAuditLog serviceAuditLog;
		public Sys_UserController(IServiceSysUser _service,
			IServiceSysAuditLog _serviceAuditLog,
			ILogger<Sys_UserController> _logger) : base(_logger)
		{
			service = _service;
			serviceAuditLog = _serviceAuditLog;
		}

		#region 标准接口
		#endregion

		#region 扩展接口
		/// <summary>
		/// 网页端登录
		/// </summary>
		/// <returns></returns>
		[HttpPost("login")]
		public IActionResult Login([FromBody]VM_Login info)
		//public IActionResult Login([FromBody]VM_LoginCaptcha info)
		{
			CheckParams();

			if (info == null)
			{
				throw new ArgumentException("用户名或密码不能为空");
			}

			UserSession userSession = service.Login(info.ucode, info.upasswd);
			string authSessionBase64Str = string.Empty;
			//authSessionBase64Str = userSession.WriteToResponseCookie(Response);
			JsonResultBase respRet = new JsonResultBase(new
			{
				skey = authSessionBase64Str,
				ucode = userSession.UserCode,
				name = userSession.UserName,
				avatar = userSession.Avatar,
				lastdate = userSession.LastLoginDate,
				lastip = userSession.LastLoginIP
			});

			return respRet;
		}

		/// <summary>
		/// 网页端退出
		/// </summary>
		/// <returns></returns>
		[HttpPost("logout")]
		public IActionResult Logout()
		{
			Response.Cookies.Delete("skey");
			Response.Cookies.Delete("ucode");
			return new JsonResultBase(true);
		}

		/// <summary>
		/// 修改密码
		/// </summary>
		/// <param name="vm"></param>
		/// <returns></returns>
		[HttpPost("changepasswd")]
		public IActionResult ChangePasswd([FromBody]VM_ChangePasswd vm)
		{
			CheckParams();

			if (vm == null)
			{
				throw new ArgumentException("新、旧密码不能为空");
			}

			UserSession sessionCurrent = GetSessionFromRequest();
			bool success = service.ChangePasswd(vm.oldpasswd, vm.newpasswd, sessionCurrent);

			return new JsonResultBase(success);
		}

		/// <summary>
		/// 当前用户信息
		/// </summary>
		/// <returns></returns>
		[HttpPost("my")]
		public IActionResult ViewMyDetail()
		{
			try
			{
				UserSession sessionCurrent = GetRawSessionFromRequest();

				var result = service.GetDetailExt(sessionCurrent);

				return new JsonResultBase(result);
			}
			catch (AuthException ex)
			{
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.Unauthorized);
			}
			catch (BuzException ex)
			{
				return new JsonResultBase(false, ex.Message);
			}
			catch (ArgumentException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.BadRequest);
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, "请求出错", (int)HttpStatusCode.InternalServerError);
			}
		}
		#endregion
	}
}